package com.atguigu.meinian.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.config.OssConfig;
import com.atguigu.meinian.constant.RedisConstant;
import com.atguigu.meinian.service.OrdersettingService;
import com.atguigu.meinian.util.MeiNianUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * @author zbystart
 * @create 2021-06-22 18:26
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrdersettingService ordersettingService;

    private Logger log = LoggerFactory.getLogger(ClearImgJob.class);

    /**
     * 当前的定时任务是在每一天的凌晨2点执行的
     * 统计的是前一天的数据，也应该删除前一天的数据
     * // 获取前一天的目录
     * //            String time = MeiNianUtils.getTheDayBefore();
     */


    public void clearImg() {
        // 定时清理垃圾图片
        log.info("开始执行定时清理垃圾图片任务!");
        Set<String> imageNames = jedisPool.getResource().sdiff(RedisConstant.ALIYUNIMAGENAMES, RedisConstant.SETMEALIMAGENAMES);
        if (imageNames != null && imageNames.size() > 0) {
            ArrayList<String> images = new ArrayList<>(imageNames);

            log.info("删除的图片：" + images.toString());
            MeiNianUtils.deleteImages(OssConfig.endPoint,OssConfig.accessKeyId,OssConfig.accessKeySecret,OssConfig.bucketName,images);
            // 删除redis中的垃圾图片
            for (String image : images) {
                jedisPool.getResource().srem(RedisConstant.ALIYUNIMAGENAMES,image);
            }
        }
        log.info("执行结束");
    }

    /**
     * 定时清除上一年的预约数据
     */
    public void clearOrdersettingInfo() {
        // 把前一年的预约数据清除掉
        ordersettingService.deletePrevYearOrdersettingInfo();
    }
}
